var HighScore = Class
		.extend({
			init : function() {
				this.baseUrl = 'http://bashibozuk.eu/games-score/?route=high-score/';
			},
			isHighScoreCallback : function(result) {
				console.log(result);
			},
			getHighScoreCallback : function(result) {
				console.log(result);
			},
			saveHighScoreCallback : function(result) {
				console.log(result);
			},
			callApi : function(method, params) {
				var src = this.baseUrl + method + '&gameId='
						+ HighScore.GAME_ID + '&jsonp=1';
				var parameters = [];
				for (i in params) {
					parameters.push(i + '=' + encodeURIComponent(params[i]));
				}

				if (parameters.length) {
					src += '&' + parameters.join('&');
				}
				src += "&_=" + Date.now();
				if (!$('#api-script-tag').length) {
					$('<script>').attr({
						src : src,
						id : 'api-script-tag'

					}).appendTo($('head'));
				} else {
					$('#api-script-tag').attr('src', src);
				}
			},
			isHighScore : function(score) {

				this.callApi('is-high-score', {
					'score' : score,
					'callback' : 'highScore.onIsHighScore'
				});
			},
			onIsHighScore : function(object) {

				console.log(object.data.isHighScore);
				if (object.data.isHighScore) {
					return true;
				} else {
					return false;
				}
			},
			setHighScore : function(score, player) {
				this.callApi('save-high-score', {
					'score' : score,
					'player' : player,
					'callback' : 'highScore.getHighScore'
				});
				console.log("lognato e");
				window.highScore.getHighScore();
			},
			getHighScore : function() {
				console.log("here");
				$
						.ajax({
							url : "http://bashibozuk.eu/games-score/?route=high-score/get-high-score&gameId=eccbc87e4b5ce2fe28308fd9f2a7baf3&jsonp=1",

							// the name of the callback parameter, as specified
							// by the YQL service
							jsonp : "callback",

							// tell jQuery we're expecting JSONP
							dataType : "jsonp",

							// tell YQL what we want and that we want JSON
							data : {

								format : "json"
							},

							// work with the response
							success : function(response) {
								console.log(response); // server response
								$("input").remove();
								$("button").remove();

								context.clearRect(0,0,2000,2000)
								var grd;

								// Add colors
								grd = ctx.createLinearGradient(0.000, 19.020,
										706.000, 297.980);

								// Add colors
								grd.addColorStop(0.000,
										'rgba(255, 0, 0, 1.000)');
								grd.addColorStop(0.150,
										'rgba(255, 0, 255, 1.000)');
								grd.addColorStop(0.330,
										'rgba(0, 0, 255, 1.000)');
								grd.addColorStop(0.490,
										'rgba(0, 255, 255, 1.000)');
								grd.addColorStop(0.670,
										'rgba(0, 255, 0, 1.000)');
								grd.addColorStop(0.840,
										'rgba(255, 255, 0, 1.000)');
								grd.addColorStop(1.000,
										'rgba(255, 0, 0, 1.000)');

								context.font = "70px Defused";
								context.fillStyle = "white";
								context.fillText("HighScore:", 500, 90);
								context.fillStyle = grd;
								context.font = "50px verdana"
								var down = 150;

								for (var i = 0; i < 10; i++) {
									context
											.fillText((i + 1) + ":"
													+ response.data[i].player
													+ " - "
													+ response.data[i].score,
													500, down);
									down += 60;
									console.log(i);
								}
							}
						});
			},

		})

HighScore.GAME_ID = 'eccbc87e4b5ce2fe28308fd9f2a7baf3';
